<?php

namespace app\common\job;

use app\admin\model\mycurrency\Device;
use app\common\library\device\cabinet\Analysis;
use app\common\library\device\cabinet\Control;
use app\common\model\mycurrency\DeviceInstruction;
use app\common\model\mycurrency\DeviceLattice;
use app\common\model\mycurrency\LeaseDeposit;
use app\common\model\mycurrency\LeaseOrder;
use app\common\model\mycurrency\LeaseOrderDevice;
use app\common\model\mycurrency\LeaseOrderPay;
use think\Db;
use think\Exception;
use think\Log;
use think\Queue;
use think\queue\Job;

class Lease
{

      /**
       * 租赁押金支付超时处理
       * @param Job $job
       * @param $data
       * @throws \think\exception\DbException
       */
      public function depositPaymentVerification(Job $job, $data)
      {
            if ($job->attempts() > 3) {
                  $job->delete();
            }
            $deposit_id = $data['deposit_id'];
            $deposit = LeaseDeposit::get($deposit_id);
            if ($deposit) {
                  if ($deposit['status'] == LeaseDeposit::STATUS_WEIZHIFU) {
                        $deposit->status = LeaseDeposit::STATUS_ZHIFUCHAOSHI;
                        $deposit->updatetime = time();
                        $deposit->save();
                        trace(["9========================处理lease_order_id报错问题_authorizeSuccess depositPaymentVerification==============================" => $deposit], 'error');
                        if ($deposit->lease_order_id != null) {//如果有订单id，表示这条押金记录是与订单绑定的顺带将订单取消掉
                              trace(["10========================处理lease_order_id报错问题_depositPaymentVerification 2==============================" => $deposit], 'error');
                              LeaseOrder::orderCancell($deposit->lease_order_id, LeaseOrder::CANCELL_TYPE_YAJINCHAOSHI);
                        }
                  }
            }
            $job->delete();
      }

      /**
       * 租赁发送开门指令 - 因为用户租赁开门前需要先查询一下隔扣内杆子的卡id，在设备上报卡id的协议中接着开门无法收到开门后的结果通知，所以放到队列中做一个延时执行
       * @param Job $job
       * @param $data
       * @throws \think\exception\DbException
       */
      public function orderStartRenting(Job $job, $data)
      {
            if ($job->attempts() > 3) {
                  $job->delete();
            }
            trace(["11========================处理lease_order_id报错问题_orderStartRenting==============================" => $data], 'error');
            $lease_order_id = $data['lease_order_id'];
            //开始开锁
            LeaseOrder::orderStartRenting($lease_order_id);
            $job->delete();
      }

      /**
       * 租赁费用支付超时处理
       * @param Job $job
       * @param $data
       * @throws \think\exception\DbException
       */
      public function payPaymentVerification(Job $job, $data)
      {
            if ($job->attempts() > 3) {
                  $job->delete();
            }
            $pay_id = $data['pay_id'];
            $pay = LeaseOrderPay::get($pay_id);
            if ($pay) {
                  if ($pay['pay_status'] == LeaseOrderPay::PAY_STATUS__DAIZHIFU) {
                        $pay->pay_status = LeaseOrderPay::PAY_STATUS__QUXIAO;
                        $pay->invalid_time = time();
                        $pay->updatetime = time();
                        $pay->save();
                        if ($pay->pay_opportunity == LeaseOrderPay::PAY_OPPORTUNITY_SHIYONGQIAN) {//如果支付的是先用的费用，同步取消订单
                              trace(["12========================处理lease_order_id报错问题_payPaymentVerification==============================" => $pay], 'error');
                              LeaseOrder::orderCancell($pay->lease_order_id, LeaseOrder::CANCELL_TYPE_ZUJINZHIFUCHAOSHI);
                        }
                  }
            }
            $job->delete();
      }

      /**
       * 归还关门超市定时检查
       * @param Job $job
       * @param $data
       * @throws \think\exception\DbException
       */
      public function returnInspect(Job $job, $data)
      {
            if ($job->attempts() > 3) {
                  $job->delete();
            }
            $lease_order_device_id = $data['lease_order_device_id'];
            $lease_order_device = LeaseOrderDevice::where(['id' => $lease_order_device_id, 'deletetime' => null])->find();
            if ($lease_order_device) {
                  if ($lease_order_device->status == LeaseOrderDevice::STATUS_GUIHUANZHONG) {//状态还是归还中
                        //发送指令查询柜门状态
                        $device_lattice = DeviceLattice::get($lease_order_device->device_lattice_id);
                        $device = Device::get($lease_order_device->device_id);
                        $data = "{$device_lattice['number']}";
                        Control::sending($device->id, Analysis::CODE_CHAXUNZHUANGTAI, $data, [
                              'device_lattice_id' => $device_lattice->id,//柜门id
                              'control_type' => DeviceInstruction::CONTROL_TYPE_GUIHUANGUANMENCHAOSHI,//控制类型 = 租赁订单归还关门超时发送指令查询柜门状态
                              'lease_order_device_id' => $lease_order_device->id,//用户租赁订单设备使用记录表id
                        ]);
                  }
            }
            $job->delete();
      }

      /**
       * 租赁订单归还后支付处理
       * @param Job $job
       * @param $data
       * @throws \think\exception\DbException
       */
      public function orderEndAfter(Job $job, $data)
      {
            if ($job->attempts() > 3) {
                  $job->delete();
            }
            $order_id = $data['order_id'];
            $pending_payment = $data['pending_payment'];
            $order = LeaseOrder::get($order_id);
            if ($order) {
                  if ($order->use_status == LeaseOrder::USE_STATUS_ZHIFUHOUFU) {
                        LeaseOrder::orderEndAfter($order_id, $pending_payment);
                  }
            }
            $job->delete();
      }

      /**
       * 租赁订单支付后分成处理
       * @param Job $job
       * @param $data
       * @throws \think\exception\DbException
       */
      public function orderDivideInto(Job $job, $data)
      {
            if ($job->attempts() > 3) {
                  $job->delete();
            }
            $order_id = $data['order_id'];
            $order = LeaseOrder::get($order_id);
            if ($order) {
                  if ($order->is_divideinto == LeaseOrder::IS_DIVIDEINTO_WEIFENCHENG) {
                        LeaseOrder::orderDivideinto($order_id);
                  }
            }
            $job->delete();
      }

      /**
       * 归还流程检测-发送检测指令，包括 查询门状态 查询杆子卡id 查询红外遮挡情况
       * @param Job $job
       * @param $data
       * @throws \think\exception\DbException
       */
      public function detectionSending(Job $job, $data)
      {
            if ($job->attempts() > 3) {
                  $job->delete();
            }
            $detection_type = $data['detection_type'];
            $order_device_id = $data['order_device_id'];
            LeaseOrderDevice::detectionSending($detection_type, $order_device_id);
            $job->delete();
      }

}